package com.linzhaowei.headhuntingservice;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hyphenate.easeui.EaseConstant;
import com.linzhaowei.headhuntingservice.bean.Delivery;
import com.linzhaowei.headhuntingservice.bean.JobInformation;
import com.linzhaowei.headhuntingservice.utils.HttpUtils;
import com.linzhaowei.headhuntingservice.utils.Ip;

import java.io.IOException;

public class JobInformationDetail extends AppCompatActivity implements View.OnClickListener {

    TextView text;
    TextView salary;
    TextView address;
    TextView name;
    TextView position;
    TextView company;
    TextView information;
    Button chat;
    Button button;
    int id;

    JobInformation jobInformation;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_job_information_detail);
        initViews();
        setEvents();
        id = getIntent().getIntExtra("job_information_id", -1);
        getJobInformationDetail();
        checkDeliver();
    }

    void initViews() {
        text = findViewById(R.id.j_i_d_text);
        salary = findViewById(R.id.j_i_d_salary);
        address = findViewById(R.id.j_i_d_address);
        name = findViewById(R.id.j_i_d_name);
        position = findViewById(R.id.j_i_d_position);
        company = findViewById(R.id.j_i_d_company);
        information = findViewById(R.id.j_i_d_information);
        chat = findViewById(R.id.j_i_d_chat);
        button = findViewById(R.id.j_i_d_deliver);
    }

    void setEvents() {
        chat.setOnClickListener(this);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.j_i_d_chat:
                Intent intent = new Intent(JobInformationDetail.this, Chat.class);
                intent.putExtra(EaseConstant.EXTRA_USER_ID, jobInformation.getUser_name());
                startActivity(intent);
                break;
            case R.id.j_i_d_deliver:
                Intent intent2 = new Intent(JobInformationDetail.this, Deliver.class);
                SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
                Delivery delivery = new Delivery();
                delivery.setApplicantId(String.valueOf(sharedPreferences.getInt("id", -1)));
                delivery.setInterviewerId(String.valueOf(jobInformation.getUser_id()));
                delivery.setJobInformationId(String.valueOf(jobInformation.getId()));
                intent2.putExtra("delivery", delivery);
                startActivity(intent2);
                break;
        }
    }

    void getJobInformationDetail() {
        new Thread(() -> {
            HttpUtils httpUtils = new HttpUtils();
            try {
                String result = httpUtils.get(Ip.ip + "/user/getJobInformationDetail?id=" + id);
                runOnUiThread(() -> show(result));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }

    void show(String result) {
        JSONArray jsonArray = JSON.parseArray(result);
        JSONObject jsonObject = jsonArray.getJSONObject(0);
        jobInformation = jsonObject.toJavaObject(JobInformation.class);

        text.setText(jobInformation.getName());
        salary.setText(String.valueOf(jobInformation.getSalary()));
        address.setText(jobInformation.getAddress());
        name.setText(jobInformation.getInterviewer_name());
        position.setText(jobInformation.getPosition());
        company.setText(jobInformation.getCompany());
        information.setText(jobInformation.getInformation());


    }

    void checkDeliver() {
        new Thread(() -> {
            HttpUtils httpUtils = new HttpUtils();
            try {
                SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
                String result = httpUtils.get(Ip.ip + "/user/checkDeliver?id=" + id + "&userId=" + sharedPreferences.getInt("id", -1));
                if (result.equals("true")) {
                    runOnUiThread(() -> {
                        button.setText("已经投递");
                        button.setOnClickListener(null);
                    });
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }
}
